/*********
SUMMARIZER
**********/

function Summarizer(){
	this.columns = {};
	this.items = {};
}

Summarizer.prototype.increment = function(row, column, appendFn){
	//console.log("Result is "+column);
	this.columns[column] = "";
	var rObj = this.items[row];
	if(!rObj){
		rObj = this.items[row] = {};
	}
	var oVal = rObj[column];
	/*
	if(!oVal)
		rObj[column] = 1;
	else
		rObj[column] = ++oVal;
	*/
	rObj[column] = appendFn(oVal);
}

Summarizer.prototype.getColumns = function(){
	this.columns["rowheader"] = "";
	var retArray = new Array();
	for(keys in this.columns){
		retArray.push(keys);
	}
	return retArray.reverse();
}

Summarizer.prototype.getItems = function(defaultVal){
	var retArray = new Array();
	for(rowName in this.items){
		var rowObj = this.items[rowName];
		rowObj["rowheader"] = rowName;
		
		for(column in this.columns){
			if(!rowObj.hasOwnProperty(column))
				rowObj[column] = defaultVal;
		}
		
		retArray.push(rowObj);
	}
	return retArray;
}
/*********************************************************************/

/*************
MISCELLANEOUS
**************/

function hasValue(a){
	return (a.join("") === "")? false : true;
}

function printQ(val){
	console.log("Start-------------");
	$j.each(val, function(i,e){
		console.log(e.toString());
	});
}

function getIDFromURL(url){
	return url.split('/').pop().replace('.js', '');
}

function upsert(hash, key, fn){
	//console.log("searching for key "+ key);
	var retrieved = hash[key];
	var retVal = fn(retrieved);
	//console.log(key + " : " + retVal);
	if (retVal)
		hash[key] = retVal;
}

function pushAll(destArr, srcArr){
	if(!srcArr)
		return srcArr; // If srcArr == null then return null
	if(!destArr)
		destArr = [];
	$j.each(srcArr, function(idx, elem){
							destArr.push(elem);
	});
	return destArr;
}

/*********************************************************************/

/***************
THROTTLE-QUERIES
****************/

function throttleQueries(queriesArr, limit, joinerfn, qbuilderfn, proceedfn){
	var resultObj = {};
	var resultsArr = [];
	var procfn = function(qArr){
		var takenQueries = qArr.slice(0, limit);
		qArr = qArr.slice(limit);
		var queryObj = joinerfn(takenQueries);
		var queryConfig = qbuilderfn(queryObj);
		var rallyDataSource = new rally.sdk.data.RallyDataSource('__WORKSPACE_OID__', 
															'__PROJECT_OID__',  
															'__PROJECT_SCOPING_UP__', 
															'__PROJECT_SCOPING_DOWN__');
		rallyDataSource.findAll(queryConfig, function(results){
			handlefn(qArr, results);
		});
	};
	var handlefn = function(arr, rs){
		resultsArr.push(rs);
		if(hasValue(arr))
			procfn(arr);
		else {
			var resultsObj = {};
			$j.each(resultsArr, function(idx, resultPiece){				//iterate thru each resultPiece array element
				$j.each(resultPiece, function(prop, values){			//iterate thru each property and the array of values it has
					upsert(resultsObj, prop, function(existing){		//upsert it into the resultsObj hash by appending to the 
						return pushAll(existing, values);				//values of each hash value
					});
				});
			});
			proceedfn(resultsObj);
		}
	};
	procfn(queriesArr);
}